package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class OrderTask {

    @Autowired
    OrderMapper orderMapper;

    /**
     * 定时处理一直处于派送中订单
     */
    @Scheduled(cron = "0 0 18 * * ?")//每天的下午六点触发一次
    public void diliverProcessOrder(){
        log.info("{}:定时处理一直处于派送中订单", LocalDateTime.now());

        //获取方法执行时前一小时的时间点
        LocalDateTime time = LocalDateTime.now().plusHours(-1);

        //查询一小时前所有还处于派送中的订单
        List<Orders> ordersList = orderMapper.findByStatusAndOrdertime(Orders.DELIVERY_IN_PROGRESS,time);

        //将查询到的订单状态改为已完成
        if (ordersList != null && ordersList.size() > 0) {
            for (Orders orders : ordersList) {
                orders.setStatus(Orders.COMPLETED);
                orderMapper.update(orders);
            }
        }
    }

}
